package com.l024.loanthief.adapter;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/18 13:17
 * @Notes
 */
public class CommonViewHolder extends RecyclerView.ViewHolder {

    //子View集合
    private SparseArray<View> mViews;
    private View mContentView;

    public CommonViewHolder(@NonNull View itemView) {
        super(itemView);
        mViews = new SparseArray<View>();
        mContentView = itemView;
    }

    /**
     * getViewHolder
     * @param viewGroup
     * @param layoutId
     * @return
     */
    public static CommonViewHolder getViewHolder(ViewGroup viewGroup, int layoutId){
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutId,null);
        return new CommonViewHolder(view);
    }

    /**
     * 提供外部访问View
     */
    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if(view==null){
            view = mContentView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }

    /**
     * 设置文本
     *
     * @param viewId
     * @param text
     * @return
     */
    public CommonViewHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }
}
